package com.mlody;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.SwingUtilities;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

import com.mlody.auction.Auction;
import com.mlody.auction.AuctionMessageTranslator;
import com.mlody.auction.AuctionSniper;
import com.mlody.auction.XMPPAuction;

public class Main {
    protected static final String MAIN_WINDOW_NAME = "MAIN_WINDOW_NAME";
    public static String SNIPER_STATUS_NAME = "SNIPER_STATUS_NAME";
    public static final String STATUS_JOINING = "STATUS_JOINING";
    public static final String STATUS_LOST = "STATUS_LOST";
    private static final int ARG_HOSTNAME = 0;
    private static final int ARG_USERNAME = 1;
    private static final int ARG_PASSWORD = 2;
    private static final int ARG_ITEM_ID = 3;
    public static final String AUCTION_RESOURCE = "Auction";
    public static final String ITEM_ID_AS_LOGIN = "auction-%s";
    public static final String AUCTION_ID_FORMAT = ITEM_ID_AS_LOGIN + "@%s/"
	    + AUCTION_RESOURCE;
    public static final String JOIN_COMMAND_FORMAT = "JOIN_COMMAND_FORMAT";
    public static final String BID_COMMAND_FORMAT = "BID_COMMAND_FORMAT";

    @SuppressWarnings("unused")
    private Chat notToBeGCd;
    static MainWindow ui;

    public Main() throws Exception {
	startUserInterface();
    }

    public static void main(String... args) throws Exception {
	Main main = new Main();
	main.joinAuction(
		connectTo(args[ARG_HOSTNAME], args[ARG_USERNAME],
			args[ARG_PASSWORD]), args[ARG_ITEM_ID]);
    }

    private void joinAuction(XMPPConnection connection, String itemId) {
	disconnectWhenUICloses(connection);
	final Chat chat = connection.getChatManager().createChat(
		auctionId(itemId, connection), null);
	this.notToBeGCd = chat;
	Auction auction = new XMPPAuction(chat);
	chat.addMessageListener(
		new AuctionMessageTranslator(
		connection.getUser(),
		new AuctionSniper(auction, new SniperStateDisplayer())));
		auction.join();

    }

    private static XMPPConnection connectTo(String hostname, String username,
	    String password) throws XMPPException {
	XMPPConnection connection = new XMPPConnection(hostname);
	connection.connect();
	connection.login(username, password, AUCTION_RESOURCE);
	return connection;
    }

    private static String auctionId(String itemId, XMPPConnection connection) {
	return String.format(AUCTION_ID_FORMAT, itemId,
		connection.getServiceName());
    }

    private void startUserInterface() throws Exception {
	SwingUtilities.invokeAndWait(new Runnable() {
	    public void run() {
		ui = new MainWindow();
	    }
	});
    }

    private void disconnectWhenUICloses(final XMPPConnection connection) {
	ui.addWindowListener(new WindowAdapter() {
	    @Override
	    public void windowClosed(WindowEvent e) {
		connection.disconnect();
	    }
	});
    }

    public void auctionClosed() {
	SwingUtilities.invokeLater(new Runnable() {
	    public void run() {
		ui.showStatus(Main.STATUS_LOST);
	    }
	});

    }

  

}
